﻿using CoreModule;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace UIModule.ViewModels
{
    /// <summary>
    /// 历史 视图 vm
    /// </summary>
    public class HistoryViewModel
        : BindableBase,
        INavigationAware,
        IRegionMemberLifetime
        
    {
     
        public bool KeepAlive => false;
    
   
        /// <summary>
        /// 测量开始时间
        /// </summary>
        public DateTime? BeingMeasurementTime { get; set; }
        /// <summary>
        /// 测量结束时间
        /// </summary>
        public DateTime? EndMeasurementTime { get; set; }

        /// <summary>
        /// 下拉类别
        /// </summary>
        public List<KeyValuePair<string, BloodGlucosePeriod>> BloodGlucosePeriodItemsSource { get; set; } = new List<KeyValuePair<string, BloodGlucosePeriod>>();

        /// <summary>
        /// 测量时段 (暂时保留）
        /// </summary>
        public BloodGlucosePeriod BloodGlucosePeriod { get; set; } = BloodGlucosePeriod.None;
         
        /// <summary>
        /// 总数量
        /// </summary>
        public int EntityTotalCount { get; set; } = 0;

        /// <summary>
        /// 每页 数量
        /// </summary>
        public int DataCountPerPage { get; set; } = 10;
        /// <summary>
        /// 最大页数
        /// </summary>
        public int MaxPageCount { get; set; }

        /// <summary>
        /// 查询
        /// </summary>
        public ICommand QueryCommand => new Prism.Commands.DelegateCommand(Query);
        /// <summary>
        /// 重置
        /// </summary>
        public ICommand ResetCommand => new Prism.Commands.DelegateCommand(Reset);
         
        public ICommand PageUpdatedCommand => new Prism.Commands.DelegateCommand<object>(PageUpdated);

 
        /// <summary>
        /// 
        /// </summary>
        INotifier Notifier { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public HistoryViewModel(INotifier notifier, IWindowLifetime windowLifetime, ILocalizationProvider localizationProvider)
        { 

            this.BloodGlucosePeriodItemsSource = localizationProvider.GetBloodGlucosePeriodDropDownItems();

            this.RaisePropertyChanged(nameof(BloodGlucosePeriodItemsSource));
 
            Notifier = notifier;

            windowLifetime.RegionViewKind = RegionViewKind.History;
             
            this.UpdateStatistic();

            this.Query();
        }
      
 
        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
          
            this.BeingMeasurementTime = null;
            this.EndMeasurementTime = null;

            this.BloodGlucosePeriod = BloodGlucosePeriod.None;
          
            this.RaisePropertyChanged(nameof(this.BeingMeasurementTime));
            this.RaisePropertyChanged(nameof(this.EndMeasurementTime));
            this.RaisePropertyChanged(nameof(this.BloodGlucosePeriod));

            this.SetBloodGlucoseStatus(BloodGlucoseStatus.None);
            
            Query(1);
            ResetPaginationPageIndex();
        }
        /// <summary>
        /// 
        /// </summary>
        HandyControl.Controls.Pagination Pagination { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public void ResetPaginationPageIndex()
        {
            if (Pagination == null)
            {
                return;
            }
            Pagination.PageIndex = 1;
        }
        /// <summary>
        /// 
        /// </summary>
        public  void PageUpdated(object sender)
        {

            var paginationArgs = sender as HandyControl.Data.FunctionEventArgs<int>;
            var pageIndex = paginationArgs.Info;
            Pagination = paginationArgs.OriginalSource as HandyControl.Controls.Pagination;

          
            Query(pageIndex);
        }

        public void Query()
        {
            Query(1);
            ResetPaginationPageIndex();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        public void Query(int pageIndex=1)
        {
            var startTime=this.BeingMeasurementTime.HasValue? 
               new DateTime(this.BeingMeasurementTime.Value.Year, this.BeingMeasurementTime.Value.Month, this.BeingMeasurementTime.Value.Day,0,0,0)
                : DateTime.MinValue;

            var endTime = this.EndMeasurementTime.HasValue ?
             new DateTime(this.EndMeasurementTime.Value.Year, this.EndMeasurementTime.Value.Month, this.EndMeasurementTime.Value.Day, 23, 59, 59)
              : DateTime.MaxValue;

            this.BloodGlucoseMeasurementInfosPage =
                BloodGlucoseMeasurementInfos
             .Where(item=> (this.BeingMeasurementTime.HasValue?item.MeasurementTime>=startTime:true)
               && (this.EndMeasurementTime.HasValue ? item.MeasurementTime <= endTime : true)
               && (this.BloodGlucosePeriod!= BloodGlucosePeriod.None ? item.BloodGlucosePeriod ==this.BloodGlucosePeriod : true)
                && (this.BloodGlucoseStatus != BloodGlucoseStatus.None ? (
                this.BloodGlucoseStatus== BloodGlucoseStatus.Other?item.BloodGlucoseStatus== BloodGlucoseStatus.HI || item.BloodGlucoseStatus == BloodGlucoseStatus.LO:
                item.BloodGlucoseStatus == this.BloodGlucoseStatus) : true) )
             .Skip((pageIndex-1)*DataCountPerPage)
             .Take(DataCountPerPage)
             .ToList();


            this.EntityTotalCount =
                BloodGlucoseMeasurementInfos
                     .Count(item => (this.BeingMeasurementTime.HasValue ? item.MeasurementTime >= startTime : true)
                       && (this.EndMeasurementTime.HasValue ? item.MeasurementTime <= endTime : true)
                       && (this.BloodGlucosePeriod != BloodGlucosePeriod.None ? item.BloodGlucosePeriod == this.BloodGlucosePeriod : true)
                         && (this.BloodGlucoseStatus != BloodGlucoseStatus.None ? (
                this.BloodGlucoseStatus == BloodGlucoseStatus.Other ? item.BloodGlucoseStatus == BloodGlucoseStatus.HI || item.BloodGlucoseStatus == BloodGlucoseStatus.LO :
                item.BloodGlucoseStatus == this.BloodGlucoseStatus) : true));
             

            this.MaxPageCount = (EntityTotalCount / DataCountPerPage) + (EntityTotalCount % DataCountPerPage==0?0:1);

            this.RaisePropertyChanged(nameof(this.BloodGlucoseMeasurementInfosPage));
            this.RaisePropertyChanged(nameof(MaxPageCount));
            this.RaisePropertyChanged(nameof(EntityTotalCount));

        }
      
        /// <summary>
        /// 选择的 前景色 颜色
        /// </summary>
        public Color CheckForegroundColor { get; set; } = Color.FromRgb(0XEB, 0X1C, 0X2A);

        /// <summary>
        /// 选择的 前景色 颜色
        /// </summary>
        public Color DefaultForegroundColor { get; set; } = Colors.Black;

        /// <summary>
        /// 选择的 背景色 
        /// </summary>
        public Color CheckBackgroundColor { get; set; } = Colors.Transparent;

        /// <summary>
        /// 默认的 背景色  F1F0F5
        /// </summary>
        public Color DefaultBackgroundColor { get; set; } = Color.FromRgb(0XF1, 0XF0, 0XF5);



        public ICommand SetHighBloodGlucoseStatusCommand => new Prism.Commands.DelegateCommand(() => SetBloodGlucoseStatus(BloodGlucoseStatus.High));

        /// <summary>
        /// 未成年 #EB1C2A 
        /// </summary>
        public SolidColorBrush SetHighBloodGlucoseStatusCommandBackground { get; set; } = new SolidColorBrush(Color.FromRgb(0XF1, 0XF0, 0XF5));

        /// <summary>
        /// 
        /// </summary>
        public SolidColorBrush SetHighBloodGlucoseStatusCommandForeground { get; set; } = new SolidColorBrush(Colors.Black);
        /// <summary>
        /// 
        /// </summary>
        public Thickness SetHighBloodGlucoseStatusCommandBorderThickness { get; set; } = new Thickness(0, 0, 0, 0);

        /// <summary>
        /// 
        /// </summary>
        public ICommand SetNormalBloodGlucoseStatusCommand => new Prism.Commands.DelegateCommand(() => SetBloodGlucoseStatus(BloodGlucoseStatus.Normal));

        /// <summary>
        /// 未成年 #EB1C2A 
        /// </summary>
        public SolidColorBrush SetNormalBloodGlucoseStatusCommandBackground { get; set; } = new SolidColorBrush(Color.FromRgb(0XF1, 0XF0, 0XF5));

        /// <summary>
        /// 
        /// </summary>
        public SolidColorBrush SetNormalBloodGlucoseStatusCommandForeground { get; set; } = new SolidColorBrush(Colors.Black);
        /// <summary>
        /// 
        /// </summary>
        public Thickness SetNormalBloodGlucoseStatusCommandBorderThickness { get; set; } = new Thickness(0, 0, 0, 0);




        public ICommand SetLowBloodGlucoseStatusCommand => new Prism.Commands.DelegateCommand(() => SetBloodGlucoseStatus(BloodGlucoseStatus.Low));

        /// <summary>
        /// 未成年 #EB1C2A 
        /// </summary>
        public SolidColorBrush SetLowBloodGlucoseStatusCommandBackground { get; set; } = new SolidColorBrush(Color.FromRgb(0XF1, 0XF0, 0XF5));

        /// <summary>
        /// 
        /// </summary>
        public SolidColorBrush SetLowBloodGlucoseStatusCommandForeground { get; set; } = new SolidColorBrush(Colors.Black);
        /// <summary>
        /// 
        /// </summary>
        public Thickness SetLowBloodGlucoseStatusCommandBorderThickness { get; set; } = new Thickness(0, 0, 0, 0);

        /// <summary>
        /// 
        /// </summary>
        public ICommand SetOtherBloodGlucoseStatusCommand => new Prism.Commands.DelegateCommand(() => SetBloodGlucoseStatus(BloodGlucoseStatus.Other));

        /// <summary>
        /// 未成年 #EB1C2A 
        /// </summary>
        public SolidColorBrush SetOtherBloodGlucoseStatusCommandBackground { get; set; } = new SolidColorBrush(Color.FromRgb(0XF1, 0XF0, 0XF5));

        /// <summary>
        /// 
        /// </summary>
        public SolidColorBrush SetOtherBloodGlucoseStatusCommandForeground { get; set; } = new SolidColorBrush(Colors.Black);
        /// <summary>
        /// 
        /// </summary>
        public Thickness SetOtherBloodGlucoseStatusCommandBorderThickness { get; set; } = new Thickness(0, 0, 0, 0);

        /// <summary>
        /// 
        /// </summary>
        public BloodGlucoseStatus BloodGlucoseStatus { get; set; }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="bloodGlucoseStatus"></param>
        public void SetBloodGlucoseStatus(BloodGlucoseStatus bloodGlucoseStatus)
        {
            this.BloodGlucoseStatus = bloodGlucoseStatus;
             
            this.SetHighBloodGlucoseStatusCommandBackground = (bloodGlucoseStatus == BloodGlucoseStatus.High)
             ? new SolidColorBrush(CheckBackgroundColor)
             : new SolidColorBrush(DefaultBackgroundColor);

            this.SetHighBloodGlucoseStatusCommandForeground = (bloodGlucoseStatus == BloodGlucoseStatus.High)
                 ? new SolidColorBrush(CheckForegroundColor)
                : new SolidColorBrush(DefaultForegroundColor);
            this.SetHighBloodGlucoseStatusCommandBorderThickness = (bloodGlucoseStatus == BloodGlucoseStatus.High)
                 ? new Thickness(1, 1, 1, 1)

                : new Thickness(0, 0, 0, 0);

            this.SetNormalBloodGlucoseStatusCommandBackground = (bloodGlucoseStatus == BloodGlucoseStatus.Normal)
         ? new SolidColorBrush(CheckBackgroundColor)
         : new SolidColorBrush(DefaultBackgroundColor);

            this.SetNormalBloodGlucoseStatusCommandForeground = (bloodGlucoseStatus == BloodGlucoseStatus.Normal)
                 ? new SolidColorBrush(CheckForegroundColor)
                : new SolidColorBrush(DefaultForegroundColor);
            this.SetNormalBloodGlucoseStatusCommandBorderThickness = (bloodGlucoseStatus == BloodGlucoseStatus.Normal)
                 ? new Thickness(1, 1, 1, 1)
                : new Thickness(0, 0, 0, 0);


            this.SetLowBloodGlucoseStatusCommandBackground = (bloodGlucoseStatus == BloodGlucoseStatus.Low)
            ? new SolidColorBrush(CheckBackgroundColor)
             : new SolidColorBrush(DefaultBackgroundColor);

            this.SetLowBloodGlucoseStatusCommandForeground = (bloodGlucoseStatus == BloodGlucoseStatus.Low)
                ? new SolidColorBrush(CheckForegroundColor)
                : new SolidColorBrush(DefaultForegroundColor);
            this.SetLowBloodGlucoseStatusCommandBorderThickness = (bloodGlucoseStatus == BloodGlucoseStatus.Low)
                 ? new Thickness(1, 1, 1, 1)
                : new Thickness(0, 0, 0, 0);


            this.SetOtherBloodGlucoseStatusCommandBackground = (bloodGlucoseStatus == BloodGlucoseStatus.Other)
             ? new SolidColorBrush(CheckBackgroundColor)
              : new SolidColorBrush(DefaultBackgroundColor);

            this.SetOtherBloodGlucoseStatusCommandForeground = (bloodGlucoseStatus == BloodGlucoseStatus.Other)
                ? new SolidColorBrush(CheckForegroundColor)
                : new SolidColorBrush(DefaultForegroundColor);
            this.SetOtherBloodGlucoseStatusCommandBorderThickness = (bloodGlucoseStatus == BloodGlucoseStatus.Other)
                 ? new Thickness(1, 1, 1, 1)
                : new Thickness(0, 0, 0, 0);


            this.RaisePropertyChanged(nameof(SetHighBloodGlucoseStatusCommandBackground));
            this.RaisePropertyChanged(nameof(SetHighBloodGlucoseStatusCommandForeground));
            this.RaisePropertyChanged(nameof(SetHighBloodGlucoseStatusCommandBorderThickness));
            ///
            this.RaisePropertyChanged(nameof(SetNormalBloodGlucoseStatusCommandBackground));
            this.RaisePropertyChanged(nameof(SetNormalBloodGlucoseStatusCommandForeground));
            this.RaisePropertyChanged(nameof(SetNormalBloodGlucoseStatusCommandBorderThickness));
            ///
            this.RaisePropertyChanged(nameof(SetLowBloodGlucoseStatusCommandBackground));
            this.RaisePropertyChanged(nameof(SetLowBloodGlucoseStatusCommandForeground));
            this.RaisePropertyChanged(nameof(SetLowBloodGlucoseStatusCommandBorderThickness));

            this.RaisePropertyChanged(nameof(SetOtherBloodGlucoseStatusCommandBackground));
            this.RaisePropertyChanged(nameof(SetOtherBloodGlucoseStatusCommandForeground));
            this.RaisePropertyChanged(nameof(SetOtherBloodGlucoseStatusCommandBorderThickness));


        }

   
        /// <summary>
        /// 全部数据
        /// </summary>

        private List<BloodGlucoseMeasurementInfo> BloodGlucoseMeasurementInfos { get; set; } = new List<BloodGlucoseMeasurementInfo>();

        /// <summary>
        /// 当前页面 数据
        /// </summary>

        public List<BloodGlucoseMeasurementInfo> BloodGlucoseMeasurementInfosPage { get; set; } = new List<BloodGlucoseMeasurementInfo>();
         
        /// <summary>
        /// 
        /// </summary>
        public void UpdateStatistic()
        {
           
        }

        /// <summary>
        /// 
        /// </summary>
        public BluetoothQueryResult BluetoothQueryResult { get; set; } = BluetoothQueryResult.None;

        /// <summary>
        /// 
        /// </summary>
        public Visibility GridNotDeviceVisibility { get; set; } = Visibility.Hidden;

        /// <summary>
        /// 
        /// </summary>
        public Visibility GridNoDataVisibility { get; set; } = Visibility.Hidden;

        /// <summary>
        /// 
        /// </summary>
        public Visibility PaginationAreaVisibility { get; set; } = Visibility.Hidden;

        /// <summary>
        /// 导航
        /// </summary>
        /// <param name="navigationContext"></param>
        public  void OnNavigatedTo(NavigationContext navigationContext)
        {
           if (!navigationContext.Parameters.ContainsKey(CoreConstant.BluetoothDataPickerResultKey))
            {
                return;
            }
            
           var bluetoothDataPickerResult = navigationContext.Parameters[CoreConstant.BluetoothDataPickerResultKey] as CoreModule.BluetoothDataPickerResult;
           
            BluetoothQueryResult = bluetoothDataPickerResult.BluetoothQueryResult;

            if (bluetoothDataPickerResult.BluetoothQueryResult== BluetoothQueryResult.NotDevice)
            {
                GridNotDeviceVisibility = Visibility.Visible;


                GridNoDataVisibility = Visibility.Hidden;
                PaginationAreaVisibility = Visibility.Hidden;

            }
            else if (bluetoothDataPickerResult.BluetoothQueryResult == BluetoothQueryResult.HasDate)
            {
                
                if (bluetoothDataPickerResult.BloodGlucoseMeasurementInfos.Count == 0)
                {
                    GridNotDeviceVisibility = Visibility.Hidden;
                    GridNoDataVisibility = Visibility.Visible;
                    PaginationAreaVisibility = Visibility.Hidden;
                }
                else
                {
                    GridNotDeviceVisibility = Visibility.Hidden;
                    GridNoDataVisibility = Visibility.Hidden;
                    PaginationAreaVisibility = Visibility.Visible;
                }

                ///
                BloodGlucoseMeasurementInfos.Clear();

                BloodGlucoseMeasurementInfos.AddRange(bluetoothDataPickerResult.BloodGlucoseMeasurementInfos);

                Query(1);
            }

            this.RaisePropertyChanged(nameof(BluetoothQueryResult));
            this.RaisePropertyChanged(nameof(GridNotDeviceVisibility));
            this.RaisePropertyChanged(nameof(GridNoDataVisibility));
            this.RaisePropertyChanged(nameof(PaginationAreaVisibility));

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
             
        }
    }
}
